package queue_stack;


/**
 * 设计循环双端队列
 *
 * 数组+双指针
 *
 * 双指针链表(pre,next)
 *
 * https://leetcode.cn/problems/design-circular-deque/
 */
class MyCircularDeque {

    int data[];
    int front,last;
    int size=0;

    public MyCircularDeque(int k) {
        front=last=0;
        size=k+1;
        data=new int[size];
    }

    public boolean insertFront(int value) {
        if (isFull()){
            return false;
        }
        front=(front-1+size)%size;
        data[front]=value;

        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()){
            return false;
        }
        data[last]=value;
        last=(last+1)%size;

        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()){
            return false;
        }
        front=(front+1)%size;

        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()){
            return false;
        }
        last=(last-1+size)%size;
        return true;
    }

    public int getFront() {
        if (isEmpty()){
            return -1;
        }
        return data[front];
    }

    public int getRear() {
        if (isEmpty()){
            return -1;
        }
        return data[(last-1+size)%size];
    }

    public boolean isEmpty() {
        return front==last;
    }

    public boolean isFull() {
        return front==(last+1)%size;
    }
}

